package cn.ichiva.puppet.example.base.p1;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.impl.AbstractPuppetComponent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;

/**
 * 条件渲染,事件绑定
 */
public class L6ListenerComponent extends AbstractPuppetComponent {

    @FXML
    StringProperty counter = new SimpleStringProperty("0"){
        {
            addListener((observableValue, number, newNumber) -> {
                System.out.println("数字发生变化:" + newNumber);
            });
        }
    };

    @Override
    public CharSequence render() {
        imports(VBox.class, Label.class, Button.class);
        return """
                <VBox>
                    <Label text="${counter}" />
                    <Button text="点我" onAction="#add" />
                </VBox>
                """;
    }

    @FXML
    void add(){
        int  n =Integer.parseInt(counter.get()) + 1;
        counter.set("" + n);
    }

}
